package com.wing.bill.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.model.entity.BillOrdInformation;
import com.wing.bill.service.BillOrdInformationService;
import com.wing.common.utils.LocalDateTimeUtils;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.bill.model.entity.BillOrdInvoiceInformation;
import com.wing.bill.model.request.BillOrdInvoiceInformationForm;
import com.wing.bill.service.BillOrdInvoiceInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.time.LocalDateTime;
import java.util.HashMap;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-账单付款发票信息管理", tags = "#/bill/billOrdInvoiceInformation")
@RestController("billOrdInvoiceInformationController")
@RequestMapping("bill/admin/billOrdInvoiceInformation")
@RequiredArgsConstructor
public class BillOrdInvoiceInformationController {

    private final BillOrdInvoiceInformationService billOrdInvoiceInformationService;
    private final BillOrdInformationService billOrdInformationService;

    @ApiOperation(value = "查询所有账单付款发票信息", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "billNo", value="账单编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "startTime", value="账单开始时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "endTime", value="账单结束时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<BillOrdInvoiceInformation> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String billNo
            ,String startTime
            ,String endTime
    ){

        Page<BillOrdInvoiceInformation> pg = new Page<>(page, limit);
        QueryWrapper<BillOrdInvoiceInformation> queryWrapper = new QueryWrapper<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }

        //搜索
        if (StringUtils.isNotBlank(billNo)) {
            queryWrapper.like("bill_no", billNo);
        }
        if (StringUtils.isNotBlank(startTime)) {
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd");
            queryWrapper.ge("create_time", start);
        }
        if (StringUtils.isNotBlank(endTime)) {
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd");
            queryWrapper.le("create_time", end);
        }
        IPage<BillOrdInvoiceInformation> list = billOrdInvoiceInformationService.getBaseMapper().selectPage(pg, queryWrapper);

        HashMap map =billOrdInformationService.find(billNo);
        for(int i = 0 ;i<list.getRecords().size();i++){
            list.getRecords().get(i).setStartTime( map.get("startTime").toString());
            list.getRecords().get(i).setEndTime( map.get("endTime").toString());
            list.getRecords().get(i).setBillTotalMoney(map.get("billTotalMoney").toString());
            list.getRecords().get(i).setIncomeNumber(map.get("incomeNumber").toString());
            list.getRecords().get(i).setIncomeBank(map.get("incomeBank").toString());
            list.getRecords().get(i).setPayAccount(map.get("payAccount").toString());
            list.getRecords().get(i).setPayBank(map.get("payBank").toString());
            list.getRecords().get(i).setPayNumber(map.get("payNumber").toString());
            list.getRecords().get(i).setConfirmName(map.get("confirmName").toString());
            list.getRecords().get(i).setConfirTime(map.get("confirTime").toString());
            list.getRecords().get(i).setPendingMoney(map.get("pendingMoney").toString());
            list.getRecords().get(i).setNum(map.get("num").toString());
            if (map.get("billStatus").toString().equals(BillOrdInformation.BillStatus.insettle.getValue())) {
                list.getRecords().get(i).setBillStatus(BillOrdInformation.BillStatus.insettle.getDesc());
            }else {
                list.getRecords().get(i).setBillStatus(BillOrdInformation.BillStatus.settled.getDesc());
            }
        }



        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加账单付款发票信息", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@Validated(BillOrdInvoiceInformationForm.Add.class) BillOrdInvoiceInformationForm billOrdInvoiceInformationForm) {
        //账单表修改状态
        return billOrdInvoiceInformationService.addAll(billOrdInvoiceInformationForm);
    }




    @ApiOperation(value = "修改账单付款发票信息", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(BillOrdInvoiceInformationForm.Update.class) BillOrdInvoiceInformationForm billOrdInvoiceInformationForm) {
        BillOrdInvoiceInformation billOrdInvoiceInformation = billOrdInvoiceInformationService.getById(billOrdInvoiceInformationForm.getId());
        if(billOrdInvoiceInformation == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(BillOrdInvoiceInformation.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(billOrdInvoiceInformationForm, billOrdInvoiceInformation, copyOptions);
        if (billOrdInvoiceInformationService.updateById(billOrdInvoiceInformation)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



@ApiOperation(value = "删除账单付款发票信息", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billOrdInvoiceInformationId", value = "账单付款发票信息id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long billOrdInvoiceInformationId) {
        if (billOrdInvoiceInformationService.removeById(billOrdInvoiceInformationId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
